#include "include/ChatterBox.h"
#include "include/Common.h"

#include <QRegExp>

ChatterBox::ChatterBox(QWidget *parent) : QWidget(parent)
{
	setupUi(this);

	// Make sure that we are showing the login page when we startup:
	stackedWidget->setCurrentWidget(chatPage);

	// Instantiate our socket (but don't actually connect to anything
	// yet until the user clicks the loginButton:
	socket = new QTcpSocket(this);

	// This is how we tell Qt to call our readyRead() and connected()
	// functions when the socket has text ready to be read, and is done
	// connecting to the server (respectively):
	connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
	connect(socket, SIGNAL(connected()), this, SLOT(connected()));

	socket->connectToHost("memusoft.com", 4200);
}

// This gets called when the user clicks the sayButton (next to where
// they type text to send to the chat room):
void ChatterBox::on_sayButton_clicked()
{
	// What did they want to say (minus white space around the string):
	QString message = sayLineEdit->text().trimmed();

	// Only send the text to the chat server if it's not empty:
	if(!message.isEmpty())
	{
		socket->write(QString(message + "\n").toUtf8());
	}

	// Clear out the input box so they can type something else:
	sayLineEdit->clear();

	// Put the focus back into the input box so they can type again:
	sayLineEdit->setFocus();
}

// This function gets called whenever the chat server has sent us some text:
void ChatterBox::readyRead()
{
	// We'll loop over every (complete) line of text that the server has sent us:
	while(socket->canReadLine())
	{
		// Here's the line the of text the server sent us (we use UTF-8 so
		// that non-English speakers can chat in their native language)
		QString line = QString::fromUtf8(socket->readLine()).trimmed();

		// These two regular expressions describe the kinds of messages
		// the server can send us:

		//  Normal messges look like this: "username:The message"
		QRegExp messageRegex("^([^:]+):(.*)$");

		// Any message that starts with "/users:" is the server sending us a
		// list of users so we can show that list in our GUI:
		QRegExp usersRegex("^/users:(.*)$");

		// Is this a users message:
		if(usersRegex.indexIn(line) != -1)
		{
			// If so, udpate our users list on the right:
			QStringList users = usersRegex.cap(1).split(",");
			userListWidget->clear();
			foreach(QString user, users)
				new QListWidgetItem(QPixmap(":/user.png"), user, userListWidget);
		}
		// Is this a normal chat message:
		else if(messageRegex.indexIn(line) != -1)
		{
			// If so, append this message to our chat box:
			QString user = messageRegex.cap(1);
			QString message = messageRegex.cap(2);

			roomTextEdit->append("<b>" + user + "</b>: " + message);
		}
	}
}

// This function gets called when our socket has successfully connected to the chat
// server. (see the connect() call in the MainWindow constructor).
void ChatterBox::connected()
{
	// Flip over to the chat page:
	stackedWidget->setCurrentWidget(chatPage);

	QString user = getUserName();

	// And send our username to the chat server.
	socket->write(QString("/me:" + user + "\n").toUtf8());
}
